<?php

/** Dataxi navigation bar == stonga
 *  This code is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *  @author Henri Ranki & Jyry Kuukkanen
 *
 *  $Id: dzstonga.php 597 2006-04-03 16:15:46Z jyry $
 */


include_once("dzconst.php");
include_once("dzgeneric.php");
include_once("sogeneric.php");

/* stonga symbols to navigate in and control DataZet */
define(DZ_SYM_OK, "ok");         /* commit, accept */
define(DZ_SYM_X, "cancel");          /* close, cancel */
define(DZ_SYM_LEFT, "left");       /* browse only: previous */
define(DZ_SYM_RIGHT, "right");      /* browse only: next */
define(DZ_SYM_EDIT, "edit");       /* browse only: edit */
define(DZ_SYM_PLUS, "new");       /* new */
define(DZ_SYM_MINUS, "delete");      /* delete */
define(DZ_SYM_LIST_BROWSE, "list");  /* list/browse */
define(DZ_SYM_KILL_VIEW, "close");      /* close, view */
define(DZ_SYM_AZ, "order");         /* query only: set order by cols */
define(DZ_VIEW_LIST, "views");     /* a list of open views */
define(DZ_SYM_HELP, "help");     /* a list of open views */
define(DZ_SYM_SUSPEND, "suspend");     /* suspend button */

define(DEFAULT_QUERY_BUTTONS, DZ_SYM_OK." ".DZ_SYM_PLUS." ".DZ_SYM_KILL_VIEW);
define(DEFAULT_LOOKUP_BUTTONS, DZ_SYM_OK." ".DZ_SYM_X." ".DZ_SYM_KILL_VIEW);
define(DEFAULT_BROWSE_BUTTONS, DZ_SYM_LEFT." ".DZ_SYM_RIGHT." ".
       DZ_SYM_EDIT." ".DZ_SYM_X." ".DZ_SYM_PLUS." ".DZ_SYM_MINUS." ".
       DZ_SYM_LIST_BROWSE." ".DZ_SYM_KILL_VIEW);
define(DEFAULT_EDIT_BUTTONS, DZ_SYM_OK." ".DZ_SYM_X." ".DZ_SYM_KILL_VIEW);
define(DEFAULT_NEW_BUTTONS, DZ_SYM_OK." ".DZ_SYM_X." ".DZ_SYM_KILL_VIEW);
define(DEFAULT_PSEUDO_BUTTONS, DZ_SYM_OK." ".DZ_SYM_X);

$DefaultNewButtons = array(DZ_SYM_OK, DZ_SYM_X, DZ_SYM_KILL_VIEW);

function dzStongaItem($Name, $Tip = "", $Caption = "") {
    if (!$Caption) $Caption = $Name;

    if ($Name) {
        return "\n<TD WIDTH=1><INPUT TYPE=BUTTON TITLE='".$Tip."' ".
               "NAME=".$Name." VALUE='".$Caption."' ".
               "OnClick=\\\"top.nalle.buttonWrapper('stonga','".$Name."');".
               "\\\"></TD>";
    } else {
        return "\n<TD WIDTH=".$Tip."%></TD>";
    };
}; // dzStongaItem


/** Generic stonga list box builder.
 *  This prints a list of open views.
 *  @param array/string $Titles List items to print.
 *  @param array/string $Values List items return by the list.
 *  @param string $Type List type, 'SUBARI' or 'VIEWLIST'.
 *  @param string $Caption List title.
 *  @result string List HTML source
 */
function dzStongaList($Type, $Titles, $Values, $Caption = "") {
    if ($Values == array()) $Values = $Titles;
    if (!$Caption) $Caption = $Type;
    if ($Type == "SUBARI") {
        $wrapper = "subari";
        $event = "ESubari";
    } else {
        $wrapper = "stonga";
        $event = "EChangeView";
    }; // else if

    soDebug("dzStongaList($Type): List count=".count(Titles), SOD_DET);
    $result = "<TD><SELECT NAME='$Type' ".
              "OnChange=\\\"top.nalle.selectWrapper('$wrapper','$event',".
              "this);\\\">\n".
              "<OPTION VALUE=''>$Caption\n".
              "<OPTION VALUE=''>\n";
    $icount = count($Titles);
    for ($i = 0; $i < $icount; $i++) {
        $result .= "<OPTION VALUE='".$Values[$i]."'>".
                   ($i +1)." ".$Titles[$i]."\n";
    }// for

    return $result."</OPTION></SELECT></TD>\n";
}; // dzStongaList


/** Prints the mode title 
 *  @param string $ModeTitle Mode's title.
 *  @return string HTML source fopr tode title string.
 */
function dzStongaMode($ModeTitle) {
    return "<TD VALIGN=CENTER><B>$ModeTitle</B></TD>\n";
}; // dzStongaMode


/** Gets buttons.
 *  @param string $Lang language code
 *  @param array/DZ_SUM_* $Items Buttom items.
 *  @return string HTML/JS code for stong buttons
 */
function dzStongaButtons($Lang, $Items) {
    $Lang = strtolower($Lang);
    if (!$Lang) $Lang = "default";
    include("dzstonga.inc");

    $symbols = array(DZ_SYM_OK, DZ_SYM_X, DZ_SYM_LEFT, DZ_SYM_RIGHT,
                     DZ_SYM_EDIT, DZ_SYM_PLUS, DZ_SYM_MINUS,
                     DZ_SYM_KILL_VIEW, DZ_SYM_SUSPEND,
                     DZ_SYM_LIST_BROWSE, DZ_SYM_HELP, DZ_SYM_AZ);
    $events = array(DZSE_COMMIT, DZSE_ROLLBACK, DZSE_PREVIOUS, DZSE_NEXT,
                    DZSE_EDIT, DZSE_NEW, DZSE_DELETE,
                    DZSE_KILLVIEW, DZSE_SUSPEND,
                    DZSE_LISTBROWSE, DZSE_HELP, DZSE_ORDERBY);

    $result = "";
    $icount = count($Items);
    $captions = $DZ_STONGA_BUTTONS[$Lang];
    for ($i = 0; $i < $icount; $i++) {
        $pos = soArrayFind($symbols, $Items[$i]);
        if ($pos != -1) {
            $result .= dzStongaItem($events[$pos], "",
                                    $captions[$events[$pos]]);
        };
    }; // for $i

    $result .= dzStongaItem("", 10);
    return $result;
}; // dzStongaButtons


/** Creates the stonga that is shown when request is sent from client to Asema
 *  @param dzTemplate $Template Template of the view
 *  @return string HTML code for Wait stonga msg.
 */
function createWaitStonga($Template) {
    $lang = $Template->SessionInfo->Lang;

    /* Produce the stonga html */
    $html = soExplode("<HTML>".
            __dzGetStongaStyleCode(DZCSS_WAIT).
            "<BODY><FORM>\n".
            "<TABLE WIDTH=100%><TR VALIGN=center><TD>\n".
            dzGetSysMsg(basename(__FILE__), "to_asema", $lang).
            "</TD></TR></TABLE></FORM></BODY></HTML>", "\n");

    /* Create the code that sets the stonga html to its frame. */
    return "function showWaitMsg() {\n".
           "StongaPtr = parent.stonga.document;\n".
           "StongaPtr.writeln(\"".implode(" ", $html)."\");\n".
           "StongaPtr.close();\n}\n";
}; // createWaitStonga


/** Helper func to return some often used html code.
 *  @param string $StyleSheet Style sheen name (DZCSS_* contants), either
 *  DZCSS_BAR or DZCSS_WAIT.
 */
function __dzGetStongaStyleCode($Style) {
    $site_inc = dzGetIniPath(DZID_SITE_INC_FOLDER);
    return "<HEAD>".
           dzGetStyleBlock($Style).
           "</HEAD>";
}; // __dzGetStongaStyleCode


/** Produce the main stonga that contains all the buttons,
 *  form name and open form list
 *  @param dzTemplate $Template Template of the view
 *  @return string HTML code for stonga.
 */
function dzMakeMainStonga($Template) {
    global $DefaultQueryButtons, $DefaultLookUpButtons, $DefaultBrowseButtons,
           $DefaultEditButtons, $DefaultNewButtons;
    soDebug("dzMakeMainStonga(".$Mode.")", SOD_DET);

    $lang = $Template->SessionInfo->Lang;
    if (!$lang) $lang = "default";

    $user_level = $Template->SessionInfo->UserLevel;
    $groups = $Template->SessionInfo->Groups;
    $result = "<HTML>".__dzGetStongaStyleCode(DZCSS_BAR).
              "<BODY></BODY><FORM>".
              "<TABLE WIDTH=100% cellspacing=1 cellpadding=0>".
              "<TR valign=top>";

    /* Set mode to DZM_PSEUDO if this is a peudo view */
    $form_type = $Template->getViewData(DZ_VI_TYPE);
    if ($form_type == DZ_VIEW_TYPE_PSEUDO) {
        $mode = DZM_PSEUDO;
    } else {
        $mode = $Template->getViewData(DZ_VI_MODE);
    }; // if

    /* Take the user defined buttons: "!" before the buttons list states
     * "default buttons excepts these" */
    $buttons = $Template->FormOptions->StongaButtons->getItem($mode);
    if (substr($buttons, 0, 1) == "!") {
        $buttons_off = soExplode(substr($buttons, 1), ",");
        $buttons = array();
    } else {
        $buttons_off = array();
        $buttons = soExplode($buttons, ",");
    }; // else if

    /* If there are no buttons yet use default buttons for that mode */
    switch ($mode) {
        case DZM_PSEUDO:
            $buttons = soExplode(DEFAULT_PSEUDO_BUTTONS, " ");
            $modeName = dzGetSysMsg(basename(__FILE__), "pseudo_mode", $lang);
            break;

        case DZM_QUERY:
            if (!count($buttons))
                $buttons = soExplode(DEFAULT_QUERY_BUTTONS, " ");
                $modeName = dzGetSysMsg(basename(__FILE__), "query_mode",
                                        $lang);
            break;
        case DZM_LOOKUP:
            if (!count($buttons)) {
                $buttons = soExplode(DEFAULT_LOOKUP_BUTTONS, " ");
            }; // if
            $modeName = dzGetSysMsg(basename(__FILE__), "lookup_mode", $lang);
            break;

        case DZM_BROWSE:
            if (!count($buttons)) {
                $buttons = soExplode(DEFAULT_BROWSE_BUTTONS, " ");
            }; // if

            /* If browse list not allowed (row limit exceeded) the list
             * button is not shown */
            if (!$Template->getFormOption(DZFO_ALLOW_BROWSE_LIST)) {
                $pos = soArrayFind($buttons, DZ_SYM_LIST_BROWSE);
                if ($pos != -1) soDelElement($buttons, $pos);
            } // if
            $modeName = dzGetSysMsg(basename(__FILE__), "browse_mode",
                                    $lang);
            break;

        case DZM_EDIT:
            if (!count($buttons)) {
                $buttons = soExplode(DEFAULT_EDIT_BUTTONS, " ");
            }; // if
            $modeName = dzGetSysMsg(basename(__FILE__), "edit_mode", $lang);
            break;

        case DZM_NEW:
            if (!count($buttons)) {
                $buttons = soExplode(DEFAULT_NEW_BUTTONS, " ");
            }; // if
            $modeName = dzGetSysMsg(basename(__FILE__), "new_mode",
                                        $lang);
            break;

        case DZM_NONE:
            break;

        default: // dialog buttons
            /* get the buttons defined in dialog file */
            $buttons =
                $Template->ScriptFeatureSpool->getAttrib(DZSA_BUTTON_LIST);
    }; // switch

    foreach ($buttons_off as $button) {
        $pos = soArrayFind($buttons, $button, 1);
        if ($pos != -1) soDelElement($buttons, $pos);
    }; // foreach

    /* Get the form name to put it into stonga */
    if ($modeName != "") {
        $form_name = $Template->getFormTitle();
        $form_name = soExtractVal(
            soSwapStr($form_name, "file://", "", 1, 1), ".", 1);
    } // $modeName

    /* If edit not allowed remove edit button */
    if (!$Template->ScriptFeatureSpool->getAttrib(DZSA_ALLOW_EDIT)) {
        $pos = soArrayFind($buttons, DZ_SYM_EDIT);
        if ($pos != -1) soDelElement($buttons, $pos);
    } // if

    /* If list cols not defined, remove "///" button */
    $list_cols = $Template->getFormOption(DZFO_LIST_COLS);
    if (!$list_cols->size()) {
        $pos = soArrayFind($buttons, DZ_SYM_LIST_BROWSE);
        if ($pos != -1) soDelElement($buttons, $pos);
    } // if

    if ($Template->getFormOption(DZFO_HELP) != "")  $buttons[] = DZ_SYM_HELP;

    if (dzGetIniItem(DZIS_GENERAL, DZID_ALLOW_SUSPEND, "app", "bool") &&
        ($form_type == DZ_VIEW_TYPE_DATAXI)) {
        $buttons[] = DZ_SYM_SUSPEND;
    } // if ($Template->getFormOption()

    $result .= dzStongaButtons($lang, $buttons);
    $title_ext =
        $Template->ScriptFeatureSpool->getAttrib(DZSA_STONGA_TITLE_EXT);
    $result .= dzStongaMode($modeName.$title_ext);

    /* Print the lock owner */
    $locked_by = $Template->getViewData(DZ_VI_LOCKED_BY);
    $lock_info = $Template->getViewData(DZ_VI_LOCK_INFO);
    if (($mode == DZM_BROWSE) &&
        (($lock_info == SOLS_ELSE) || ($lock_info == SOLS_LOCAL))) {
        $locked_title = dzGetSysMsg(basename(__FILE__), "locked", $lang);
        $result .= "<TD VALIGN=CENTER>$locked_title $locked_by</TD>\n";
    } // if

    /* Does subari exists for the form? If so, create the list box for it */
    if (soGetClass($Template->Subari) == "dzsubari") {
        $subari_titles =
            $Template->Subari->getTitles($mode, $groups, $user_level);
        /* Add title of subari */
        if (count($subari_titles) > 0) {
            include("dzstonga.inc");
            $captions = $DZ_CAPTIONS[$lang];
            $result .= dzStongaList("SUBARI", $subari_titles, array(),
                                    $captions[DZSC_SUBARI]);
        }; // if
    } // if (soGetClass($Template->Subari)

    $views = $Template->ScriptFeatureSpool->getAttrib(DZSA_STONGA_VIEW_LIST);

    /* If there is open views show them in a drop down menu */
    if (count($views) != 0) {
        $view_list_title .= dzGetSysMsg(basename(__FILE__),
                                        "open_views",
                                        $lang);
        $values = array();
        $vcount = count($views);
        for ($v = 0; $v < $vcount; $v++) {
            $split = soSplitStr($views[$v], "/", 1, 0);
            $values[$v] = $split[0];
            $views[$v] = $split[1];
        }; // for
        $result .= dzStongaList("STONGA", $views, $values, $view_list_title);
    }; // if

	return $result."</TR></TABLE></FORM></BODY></HTML>";
} // dzMakeMainStonga


/** Create stonga when the form is lockecf (suspended)
 *  @param dzTemplate The view data
 *  @return string The html code for suspend stonga msg
 */
function dzMakeSuspendStonga(&$Template) {
    $lang = $Template->SessionInfo->Lang;

    return "<HTML>".
           __dzGetStongaStyleCode(DZCSS_BAR).
           "<BODY><FORM><TABLE WIDTH=100%><TR>".
           dzStongaButtons($lang, array(DZ_SYM_OK)).
           "</TR></TABLE></FORM></BODY></HTML>";
} // makeSuspendStonga


/** Produces the JavaScript code that puts the stonga html code
 *  into its frame.
 *  @param dzTemplate Parent template object
 *  @return string JS code for stonga creation
 */
function dzMakeStonga(&$Template) {
    /* Create the code that produces the main stonga when
     * the form is loaded */
    $result = "StongaPtr = parent.stonga.document;\n";
    $suspend = $Template->ScriptFeatureSpool->getAttrib(DZSA_SUSPEND);
    if ($suspend) {
        $html = dzMakeSuspendStonga($Template);
    } else {
        $html = dzMakeMainStonga($Template);
    }; // if
    $html_arr = soExplode($html, "\n");
    $result .= "StongaPtr.writeln(\"".implode(" ", $html_arr)."\");\n";
    $result .= "StongaPtr.close();\n".
               "\n\n".createWaitStonga($Template);

    return $result;
} // dzMakeStonga

?>
